package com.jspxcms.core.web.back;


import com.jspxcms.common.orm.RowSide;
import com.jspxcms.common.web.Servlets;
import com.jspxcms.core.constant.Constants;
import com.jspxcms.core.domain.OperationLog;
import com.jspxcms.core.domain.Site;
import com.jspxcms.core.service.OperationLogService;
import com.jspxcms.core.support.Context;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 操作日志
 *
 */
@Controller
@RequestMapping("core/operation_log")
public class OperationLogController {
    private static final Logger logger = LoggerFactory.getLogger(OperationLogController.class);

    @RequiresPermissions("core:operation_log:list")
    @GetMapping("list.do")
    public String list(@PageableDefault(sort = "id", direction = Sort.Direction.DESC) Pageable pageable,
                       HttpServletRequest request, org.springframework.ui.Model modelMap) {
        // 获取当前站点
        Site site = Context.getCurrentSite();
        Map<String, String[]> params = Servlets.getParamValuesMap(request, Constants.SEARCH_PREFIX);
        Page<OperationLog> pagedList = operationLogService.findAll(site.getId(), params, pageable);
        modelMap.addAttribute("pagedList", pagedList);
        return "core/operation_log/oper_log_list";
    }

    @RequiresPermissions("core:operation_log:view")
    @GetMapping("view.do")
    public String view(Integer id, Integer position, @PageableDefault(sort = "id", direction = Sort.Direction.DESC) Pageable pageable,
                       HttpServletRequest request, org.springframework.ui.Model modelMap) {
        Site site = Context.getCurrentSite();
        OperationLog bean = operationLogService.get(id);
        Map<String, String[]> params = Servlets.getParamValuesMap(request, Constants.SEARCH_PREFIX);
        RowSide<OperationLog> side = operationLogService.findSide(site.getId(), params, bean, position, pageable.getSort());
        modelMap.addAttribute("bean", bean);
        modelMap.addAttribute("side", side);
        modelMap.addAttribute("position", position);
//        modelMap.addAttribute(OPRT, EDIT);
        return "core/operation_log/oper_view";
    }


    @Autowired
    private OperationLogService operationLogService;


}
